<?php
include "../connect.php";
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 10;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$date_m=array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม",);

$sql_fashion =mysql_query("SELECT ca.c_id,ca.c_section,ca.c_parent,a.c_id,a.c_category,a.c_public,a.c_title,a.c_post_on,a.c_intro,a.c_intro_2,a.c_image,a.c_post_editor,
(select c_display_name from smm_members where c_id = a.c_post_editor)n_user
from smm_article_category ca left join smm_article a on ca.c_id = a.c_category
where a.c_category='9' or ca.c_parent='9'  and a.c_public ='1' order by c_post_on DESC LIMIT $start, $per_page");
$msg = "";
$count =mysql_num_rows($sql_fashion);
while($rs=mysql_fetch_array($sql_fashion)){
    $y=date("Y",strtotime($rs['c_post_on']))+543;
    $m=$date_m[date("n",strtotime($rs['c_post_on']))];
$msg .= '<div class="data_fashion"><div class="img_fa"><a href="/detail_article.php?c_id='.$rs[c_id].'" target="_blank" title="'.$rs['c_title'].'">
<div class="images_fa"><img src='.str_replace("small","big",$rs[c_image]).' width="182" height="120" border="0"></div></a></div><div class="title_fa">
<div class="title_fa1"><a href="/detail_article.php?c_id='.$rs[c_id].'" target="_blank" title="'.$rs['c_title'].'">'.$rs[c_title].'</a></div>
<div class="intro_fa">'.$rs[c_intro].'&nbsp;'.iconv_substr($rs[c_intro_2],0,102,"utf-8").'...</div><div class="date_fa">เขียนโดย :&nbsp;'.$rs[n_user].'<br>เขียนเมื่อ :&nbsp;'.date("d $m $y เวลา H:i น.",strtotime($rs[c_post_on])).'</div></div></div><br style="clear:both;" /><div class="hr_fa"></div>';

	}



/* --------------------------------------------- */
$query_pag_num = "SELECT ca.c_id,ca.c_parent,a.c_id,a.c_category,a.c_public
from smm_article_category ca left join smm_article a on ca.c_id = a.c_category
where a.c_category='9' or ca.c_parent='9'  and a.c_public ='1'";
$result_pag_num = mysql_query($query_pag_num);
$count =mysql_num_rows($result_pag_num);

$no_of_paginations = ceil($count / $per_page);

/* ---------------Calculating the starting and endign values for the loop----------------------------------- */

if ($cur_page >= 7) {
    $start_loop = $cur_page - 3;
    if ($no_of_paginations > $cur_page + 3)
        $end_loop = $cur_page + 3;
    else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
        $start_loop = $no_of_paginations - 6;
        $end_loop = $no_of_paginations;
    } else {
        $end_loop = $no_of_paginations;
    }
} else {
    $start_loop = 1;
    if ($no_of_paginations > 7)
        $end_loop = 7;
    else
        $end_loop = $no_of_paginations;
}
/* ----------------------------------------------------------------------------------------------------------- */

$msg .= "<div class='pagination'><ul class='pag'>";

// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
    $msg .= "<li p='1' class='active'>First</li>";
} else if ($first_btn) {
    $msg .= "<li p='1' class='inactive'>First</li>";
}

// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
    $pre = $cur_page - 1;
    $msg .= "<li p='$pre' class='active'>Previous</li>";
} else if ($previous_btn) {
    $msg .= "<li class='inactive'>Previous</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {

    if ($cur_page == $i)
        $msg .= "<li p='$i' style='color:#fff;background-color:#ee2466;' class='active'>{$i}</li>";
    else
        $msg .= "<li p='$i' class='active'>{$i}</li>";
}

// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
    $nex = $cur_page + 1;
    $msg .= "<li p='$nex' class='active'>Next</li>";
} else if ($next_btn) {
    $msg .= "<li class='inactive'>Next</li>";
}

// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
    $msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
} else if ($last_btn) {
    $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
}
//$goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:30px;'/>&nbsp;<input type='button' id='go_btn' class='go_button' value='Go'/>";
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>" . $goto . $total_string . "</div>";  // Content for pagination
echo $msg;


}
?>